//
//  sfDetailsViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/21.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "sfDetailsViewController.h"
#import "Masonry.h"
#import "GPosAPI.h"
#import "WKProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "XMRegisterViewController.h"
#import "SVProgressHUD.h"

@interface sfDetailsViewController ()
@property (nonatomic,strong)UIView * topView;
@property (nonatomic,strong)UILabel *TimeLabel;
@property (nonatomic,strong)NSString * timeStr;
@property (nonatomic,strong)UIButton * zhongbudfbutton;
@property (nonatomic,strong)UILabel * dizhiLabel;
@property(strong,nonatomic)WKProgressHUD *hud;
@property (nonatomic,strong)UILabel *kjzshouyiLabel;
@property (nonatomic,strong)UILabel *zhongbufsLabel;
@property (nonatomic,strong)UILabel *zhongbuzzTextLabel;
@property (nonatomic,strong)UILabel * bottomkjsyje;
@property (nonatomic,strong)UILabel *bottomtjsyje;
@property (nonatomic,strong)NSString *qbdzArr;

@property (nonatomic,strong)NSString * amount;
@property (nonatomic,strong)NSString *accountAddress;
@property (nonatomic,strong)NSString *createTime;
@property (nonatomic,assign)int Ttype;
@property (nonatomic,strong)NSString *portrait;
@property (nonatomic,strong)NSString*oppositeName;
//@property (nonatomic,strong)UILabel * jyslabel;
@end

@implementation sfDetailsViewController

//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.title = @"账单详情";
    [self loadData];
    
    
}

- (void)loadData{
    NSDictionary *params = @{@"orderNumber" : self.orderNumber};
    
    [GPosAPI requestorderDetailWithParams:params andCallback:^(id obj) {
        NSLog(@"obj--%@",obj);
        if (obj == nil) {
            
            self.hud=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
            
        }else {
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                NSDictionary * dict = [obj objectForKey:@"data"];
                self.amount = [dict objectForKey:@"amount"];
                self.accountAddress =[dict objectForKey:@"accountAddress"];
                self.createTime = [dict objectForKey:@"createTime"];
                self.portrait = [dict objectForKey:@"portrait"];
                self.Ttype = [[dict objectForKey:@"type"] intValue];
                self.oppositeName = [dict objectForKey:@"oppositeName"];
                
                
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402){
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
               
            }else{
                
                self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
            }
            
            
        }
        
        [self topLoadView];
    }];
    
}

- (void)topLoadView {
    self.topView = [[UIView alloc] init];
    self.topView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(kNavbarHeight);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@10);
    }];
    
    
    UIView * kjsyBGView = [[UIView alloc] init];
    kjsyBGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:kjsyBGView];
    [kjsyBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).with.offset(0);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@180);
    }];
    
    UIImageView * kjImageView = [[UIImageView alloc] init];
    kjImageView.image = [UIImage imageNamed:@"icon_kjsy"];
    kjImageView.layer.cornerRadius = 20;
    kjImageView.clipsToBounds=YES;
    [kjsyBGView addSubview:kjImageView];
    [kjImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjsyBGView).with.offset(30);
        make.centerX.equalTo(kjsyBGView);
        make.height.equalTo(@50);
        make.width.equalTo(@50);
    }];
    
    UILabel * kjsytishiLabel = [[UILabel alloc] init];
    kjsytishiLabel.font = [UIFont systemFontOfSize:14];
    kjsytishiLabel.textColor = [UIColor blackColor];
    [kjsyBGView addSubview:kjsytishiLabel];
    [kjsytishiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjImageView.mas_bottom).with.offset(10);
        make.centerX.equalTo(kjsyBGView);
    }];
    
    
    self.kjzshouyiLabel = [[UILabel alloc] init];
    self.kjzshouyiLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    self.kjzshouyiLabel.textColor = [UIColor colorWithRed:70/255.0f green:85/255.0f blue:254/255.0f alpha:1];
    [kjsyBGView addSubview:self.kjzshouyiLabel];
    [self.kjzshouyiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjsytishiLabel.mas_bottom).with.offset(14);
        make.centerX.equalTo(kjsyBGView);
    }];
    kjsytishiLabel.text = self.oppositeName;
   
    
    UIView * zhongbuView = [[UIView alloc] init];
    zhongbuView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:zhongbuView];
    [zhongbuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjsyBGView.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@201.5);
    }];
    UIView * zhongbuOneView = [[UIView alloc] init];
    zhongbuOneView.backgroundColor = [UIColor grayColor];
    [zhongbuView addSubview:zhongbuOneView];
    [zhongbuOneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhongbuView).with.offset(10);
        make.right.equalTo(zhongbuView).with.offset(-10);
        make.top.equalTo(zhongbuView).with.offset(50);
        make.height.equalTo(@0.5);
    }];
    
    UIView * zhongbuTwoView = [[UIView alloc] init];
    zhongbuTwoView.backgroundColor = [UIColor grayColor];
    [zhongbuView addSubview:zhongbuTwoView];
    [zhongbuTwoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhongbuView).with.offset(10);
        make.right.equalTo(zhongbuView).with.offset(-10);
        make.top.equalTo(zhongbuOneView.mas_bottom).with.offset(50);
        make.height.equalTo(@0.5);
    }];
    
    UIView * zhongbuThreeView = [[UIView alloc] init];
    zhongbuThreeView.backgroundColor = [UIColor grayColor];
    [zhongbuView addSubview:zhongbuThreeView];
    [zhongbuThreeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhongbuView).with.offset(10);
        make.right.equalTo(zhongbuView).with.offset(-10);
        make.top.equalTo(zhongbuTwoView.mas_bottom).with.offset(50);
        make.height.equalTo(@0.5);
    }];
    
    self.zhongbufsLabel = [[UILabel alloc] init];
    self.zhongbufsLabel.font = [UIFont systemFontOfSize:14];
    self.zhongbufsLabel.textColor = [UIColor blackColor];
    [zhongbuView addSubview:self.zhongbufsLabel];
    [self.zhongbufsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhongbuView).with.offset(5);
        make.left.equalTo(zhongbuView).with.offset(15);
        make.bottom.equalTo(zhongbuOneView.mas_top).with.offset(-5);
    }];
    
    
    UILabel * zhongbufsTextLabel = [[UILabel alloc] init];
    zhongbufsTextLabel.font = [UIFont systemFontOfSize:14];
    zhongbufsTextLabel.textColor = [UIColor blackColor];
    [zhongbuView addSubview:zhongbufsTextLabel];
    [zhongbufsTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.zhongbufsLabel);
        make.right.equalTo(zhongbuView).with.offset(-15);
        
    }];
    
    
    UILabel * zhongbuzzLabel = [[UILabel alloc] init];
    zhongbuzzLabel.font = [UIFont systemFontOfSize:14];
    zhongbuzzLabel.textColor = [UIColor blackColor];
    [zhongbuView addSubview:zhongbuzzLabel];
    [zhongbuzzLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhongbuOneView.mas_bottom).with.offset(5);
        make.left.equalTo(zhongbuView).with.offset(15);
        make.bottom.equalTo(zhongbuTwoView.mas_top).with.offset(-5);
    }];
   
    
    self.zhongbuzzTextLabel = [[UILabel alloc] init];
    self.zhongbuzzTextLabel.font = [UIFont systemFontOfSize:14];
    self.zhongbuzzTextLabel.textColor = [UIColor blackColor];
    [zhongbuView addSubview:self.zhongbuzzTextLabel];
    [self.zhongbuzzTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(zhongbuzzLabel);
        make.right.equalTo(zhongbuView).with.offset(-15);
        
    }];
    
    
    UILabel * zhongbudfLabel = [[UILabel alloc] init];
    zhongbudfLabel.font = [UIFont systemFontOfSize:14];
    zhongbudfLabel.textColor = [UIColor blackColor];
    [zhongbuView addSubview:zhongbudfLabel];
    [zhongbudfLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhongbuTwoView.mas_bottom).with.offset(5);
        make.left.equalTo(zhongbuView).with.offset(15);
        make.bottom.equalTo(zhongbuThreeView.mas_top).with.offset(-5);
    }];
  
    
    self.zhongbudfbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.zhongbudfbutton setImage:[UIImage imageNamed:@"icon_biyan_h"] forState:UIControlStateNormal];
    [self.zhongbudfbutton addTarget:self action:@selector(detbuttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [zhongbuView addSubview:self.zhongbudfbutton];
    [self.zhongbudfbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(zhongbudfLabel);
        make.right.equalTo(zhongbuView).with.offset(-15);

    }];
    
    self.dizhiLabel = [[UILabel alloc] init];
    self.dizhiLabel.font = [UIFont systemFontOfSize:14];
    self.dizhiLabel.numberOfLines = 2;
    self.dizhiLabel.textColor = [UIColor blackColor];
    self.dizhiLabel.textAlignment = NSTextAlignmentRight;
    [zhongbuView addSubview:self.dizhiLabel];
    [self.dizhiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(zhongbudfLabel);
        make.left.equalTo(zhongbudfLabel.mas_right).with.offset(10);
//        make.right.equalTo(zhongbuView.mas_left).with.offset(20);
        make.width.equalTo(@200);
    }];
    
//    self.jyslabel = [[UILabel alloc] init];
//    self.jyslabel.font = [UIFont systemFontOfSize:14];
//    self.jyslabel.numberOfLines = 2;
//    self.jyslabel.textColor = [UIColor blackColor];
//    [zhongbuView addSubview:self.jyslabel];
//    [self.jyslabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(zhongbudfLabel);
//        make.right.equalTo(zhongbuView).with.offset(-15);
//        //        make.right.equalTo(zhongbuView.mas_left).with.offset(20);
//
//    }];
    
    UILabel * zhongbuflLabel = [[UILabel alloc] init];
    zhongbuflLabel.font = [UIFont systemFontOfSize:14];
    zhongbuflLabel.textColor = [UIColor blackColor];
    [zhongbuView addSubview:zhongbuflLabel];
    [zhongbuflLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhongbuThreeView.mas_bottom).with.offset(5);
        make.left.equalTo(zhongbuView).with.offset(15);
        make.bottom.equalTo(zhongbuView).with.offset(-5);
    }];
    
    
    UILabel * zhongbuflTextLabel = [[UILabel alloc] init];
    zhongbuflTextLabel.font = [UIFont systemFontOfSize:14];
    zhongbuflTextLabel.textColor = [UIColor blackColor];
    [zhongbuView addSubview:zhongbuflTextLabel];
    [zhongbuflTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(zhongbuflLabel);
        make.right.equalTo(zhongbuView).with.offset(-15);
        
    }];
    
    UIView * bottomBGView = [[UIView alloc] init];
    bottomBGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomBGView];
    [bottomBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhongbuView.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@109);
    }];
    
    UIView * bottomSXView = [[UIView alloc] init];
    bottomSXView.backgroundColor = [UIColor grayColor];
    [bottomBGView addSubview:bottomSXView];
    [bottomSXView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomBGView).with.offset(10);
        make.right.equalTo(bottomBGView).with.offset(-10);
        make.centerY.equalTo(bottomBGView);
        make.height.equalTo(@0.5);
    }];
    
    UILabel * bottomkjsyts = [[UILabel alloc] init];
    bottomkjsyts.font = [UIFont systemFontOfSize:14];
    bottomkjsyts.textColor = [UIColor grayColor];
    [bottomBGView addSubview:bottomkjsyts];
    [bottomkjsyts mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomBGView).with.offset(5);
        make.left.equalTo(bottomBGView).with.offset(15);
        make.bottom.equalTo(bottomSXView.mas_top).with.offset(-5);
    }];
    
    self.bottomkjsyje  = [[UILabel alloc] init];
    self.bottomkjsyje.font = [UIFont systemFontOfSize:14];
    self.bottomkjsyje.textColor = [UIColor blackColor];
    [bottomBGView addSubview:self.bottomkjsyje];
    [self.bottomkjsyje mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomBGView).with.offset(5);
        make.right.equalTo(bottomBGView).with.offset(-15);
        make.bottom.equalTo(bottomSXView.mas_top).with.offset(-5);
    }];
    
    UILabel * bottomtjsyts = [[UILabel alloc] init];
    bottomtjsyts.font = [UIFont systemFontOfSize:14];
    bottomtjsyts.textColor = [UIColor grayColor];
    [bottomBGView addSubview:bottomtjsyts];
    [bottomtjsyts mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomSXView.mas_bottom).with.offset(5);
        make.left.equalTo(bottomBGView).with.offset(15);
        make.bottom.equalTo(bottomBGView).with.offset(-5);
    }];
    
    self.bottomtjsyje  = [[UILabel alloc] init];
    self.bottomtjsyje.font = [UIFont systemFontOfSize:14];
    self.bottomtjsyje.textColor = [UIColor blackColor];
    [bottomBGView addSubview:self.bottomtjsyje];
    [self.bottomtjsyje mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomSXView.mas_bottom).with.offset(5);
        make.right.equalTo(bottomBGView).with.offset(-15);
        make.bottom.equalTo(bottomBGView).with.offset(-5);
    }];
    
    
    if (self.Ttype==3 ) {
//        NSNumber * nsNumber1 = @(self.amount);
        self.kjzshouyiLabel.text = [NSString stringWithFormat:@"+%@",@(self.amount.floatValue)];
        self.kjzshouyiLabel.textColor = [UIColor blueColor];
        self.zhongbufsLabel.text = @"接收方式";
        self.zhongbuzzTextLabel.text = @"接收";
    }else if (self.Ttype == 4) {
//        NSNumber * nsNumber1 = @(self.amount);
        self.kjzshouyiLabel.text = [NSString stringWithFormat:@"-%@",@(self.amount.floatValue)];
        self.kjzshouyiLabel.textColor = [UIColor grayColor];
        self.zhongbufsLabel.text = @"发送方式";
        self.zhongbuzzTextLabel.text = @"发送";
    }else if (self.Ttype==2 ) {
        self.kjzshouyiLabel.text = [NSString stringWithFormat:@"-%@",@(self.amount.floatValue)];
        self.kjzshouyiLabel.textColor = [UIColor grayColor];
        self.zhongbufsLabel.text = @"发送方式";
        self.zhongbuzzTextLabel.text = @"发送";
        
    }else if (self.Ttype==5 ) {
        self.kjzshouyiLabel.text = [NSString stringWithFormat:@"-%@",@(self.amount.floatValue)];
        self.kjzshouyiLabel.textColor = [UIColor grayColor];
        self.zhongbufsLabel.text = @"发送方式";
        self.zhongbuzzTextLabel.text = @"转币";
        
    }
    
    
    // timeStampString 是服务器返回的13位时间戳
    NSLog(@"---%@",self.createTime);
    NSString *timeStampString  = self.createTime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date  = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString    = [formatter stringFromDate: date];
    self.bottomkjsyje.text = dateString;
    
//    if (self.Ttype == 5) {
//        self.zhongbudfbutton.hidden = YES;
//        self.jyslabel.text = self.accountAddress;
//    }else {
    
        NSString * str = [self.accountAddress substringToIndex:4];
        NSString * twoStr = [self.accountAddress substringFromIndex:self.accountAddress.length-4];
        NSLog(@"str---%@, two--%@",str,twoStr);
        self.qbdzArr = [NSString stringWithFormat:@"%@****%@",str,twoStr];
        self.dizhiLabel.text = self.qbdzArr;
//    }
   
    
    self.bottomtjsyje.text = self.orderNumber;
    
     [kjImageView sd_setImageWithURL:[NSURL URLWithString:self.portrait] placeholderImage:[UIImage imageNamed:@"icon_tx_mh"]];
//    self.bottomtjsyje.text = @"1213";
     zhongbudfLabel.text = @"对方账户";
     zhongbuzzLabel.text = @"转账备注";
    zhongbuflTextLabel.text = @"交易";
    zhongbuflLabel.text = @"账单分类";
    zhongbufsTextLabel.text = @"余额";
    
    bottomkjsyts.text = @"创建时间";
    bottomtjsyts.text = @"订单号";
    
    
    
}

- (void)detbuttonAction:(UIButton *)sender{
    if([self.dizhiLabel.text rangeOfString:@"****"].location !=NSNotFound)
    {
        NSLog(@"yes");
        [self.zhongbudfbutton  setImage:[UIImage imageNamed:@"icon_yangjingh"] forState:UIControlStateNormal];
        self.dizhiLabel.text = self.accountAddress;
    }
    else
    {
        
        [self.zhongbudfbutton setImage:[UIImage imageNamed:@"icon_biyan_h"] forState:UIControlStateNormal];
        self.dizhiLabel.text = self.qbdzArr;
        NSLog(@"no");
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
